import { Module } from 'vuex'
import { IRootState } from '../types'
import { IBaseconfig, IBaseconfigState, ICompiler } from './types'
import { IDataType } from '@/render/service/types'
import {
  updateBaseConfiguration,
  getBaseConfiguration
} from '@/render/service/baseconfig/baseconfig'

const baseconfigModule: Module<IBaseconfigState, IRootState> = {
  namespaced: true,
  state() {
    return {
      baseconfigMap: new Map()
    }
  },
  getters: {
    getBaseConfiguration: (state, projectName) => {
      if (state.baseconfigMap) {
        if (state.baseconfigMap.has(projectName)) {
          return state.baseconfigMap.get(projectName)
        }
      }
      // 出错
    }
  },
  mutations: {
    SET_BASECONFIGURATION(state, { key, value }) {
      if (state.baseconfigMap == null) {
        state.baseconfigMap = new Map()
      }
      state.baseconfigMap.set(key, value)
    },
    DELETE_BASECONFIGURATION(state, key) {
      if (state.baseconfigMap?.has(key)) {
        state.baseconfigMap.delete(key)
      }
    }
  },
  actions: {
    loadBaseConfiguration({ commit, state }, projectName: string) {
      return new Promise((resolve, reject) => {
        if (
          state.baseconfigMap != null &&
          state.baseconfigMap.size > 0 &&
          state.baseconfigMap.has(projectName)
        ) {
          resolve(state.baseconfigMap.get(projectName))
        } else {
          getBaseConfiguration(projectName)
            .then((res: any) => {
              const data = res.data
              const platform = data.platform
              const version = data.version
              const compilers = data.compilers

              const baseConfiguration: IBaseconfig = {
                projectName: projectName,
                platform: platform,
                version: {
                  vendorId: version.vendorId,
                  arReleaseMajorVersion: Number(version.arReleaseMajorVersion),
                  arReleaseMinorVersion: Number(version.arReleaseMinorVersion),
                  arReleaseRevisionVersion: Number(
                    version.arReleaseRevisionVersion
                  ),
                  swMajorVersion: Number(version.swMajorVersion),
                  swMinorVersion: Number(version.swMinorVersion),
                  swPatchVersion: Number(version.swPatchVersion)
                },
                compilers: compilers.map((item: ICompiler) => item.name)
              }
              commit('SET_BASECONFIGURATION', {
                key: projectName,
                value: baseConfiguration
              })
              resolve(baseConfiguration)
            })
            .catch((error: any) => {
              reject(error)
            })
        }
      })
    },

    updateBaseConfiguration(
      { commit, state },
      { projectName, version, platform, compilers }
    ) {
      return new Promise((resolve, reject) => {
        const baseConfiguration: IBaseconfig = {
          projectName: projectName,
          version: version,
          platform: platform,
          compilers: compilers
        }
        updateBaseConfiguration(baseConfiguration)
          .then((res: any) => {
            commit('SET_BASECONFIGURATION', {
              key: projectName,
              value: baseConfiguration
            })
            resolve(res)
          })
          .catch((error: any) => {
            reject(error)
          })
      })
    }
  }
}

export default baseconfigModule
